package com.xt.wikiperfumapi.model.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xt.wikiperfumapi.model.FragranceNoteInfo;
import com.xt.wikiperfumapi.model.IngredientInfo;
import com.xt.wikiperfumapi.model.vo.FragranceNoteVo;
import com.xt.wikiperfumapi.service.impl.IngredientInfoServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class FragranceNoteWrapper {

    @Resource
    IngredientInfoServiceImpl ingredientInfoService;

    /**
     * 进行vo转换
     *
     * @param info
     * @param num  原料数量
     * @return
     */
    public FragranceNoteVo fragranceNoteToVo(FragranceNoteInfo info, int num) {
        FragranceNoteVo fragranceNoteVo = new FragranceNoteVo();
        try {
            BeanUtils.copyProperties(fragranceNoteVo, info);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("fragrance_note_id", info.getFragranceNoteId());
        wrapper.eq("is_deleted", 0);
        if(num != 0){
            wrapper.last("LIMIT "+num); // 限制查询结果为前 10 项
        }
        List<IngredientInfo> ingredientInfos = ingredientInfoService.list(wrapper);
        fragranceNoteVo.setIngredientInfoSize(ingredientInfoService.count(new QueryWrapper<IngredientInfo>().eq("fragrance_note_id", info.getFragranceNoteId()).eq("is_deleted", 0)));
        fragranceNoteVo.setIngredientInfos(ingredientInfos);
        return fragranceNoteVo;
    }

}
